package org.hr.xtime.absence

import java.text.SimpleDateFormat
import org.h2.command.dml.Update;
import org.hr.xtime.AbsenceService;
import org.hr.xtime.user.Employee;
import org.springframework.dao.DataIntegrityViolationException

class RequestController {

	static allowedMethods = [save: "POST", update: "POST", delete: "POST", saveApproval: "POST"]

	def scaffold = true

	AbsenceService absenceService

	def testDatePicker = {
	}

	def index() {
		redirect(action: "list", params: params)
	}

	def list() {
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		Employee employee = Employee.findByUserId(session.user.userId)
		def requestList = Request.findAllByRequestorAndStatusNotEqual(employee,"final")
		[requestInstanceList: requestList, requestInstanceTotal: Request.count()]
	}

	def create() {
		Date fromDate = params.fromDate?Date.parse("dd-MM-yyyy", params.fromDate):null
		Date toDate = params.toDate?Date.parse("dd-MM-yyyy",params.toDate):fromDate
		[requestInstance: new Request(params), fromDate: fromDate, toDate:toDate]
	}

	def saveCreation(){
		Employee requestor = Employee.findByUserId(session.user.userId)
		Employee approver = absenceService.getApprover(requestor.userId, new Date())
		DayType dayType = DayType.findByLabel(params.dayType)
		Date fromDate = params.fromDate?new SimpleDateFormat("dd-MM-yyyy").parse(params.fromDate):null
		Date toDate = params.toDate?new SimpleDateFormat("dd-MM-yyyy").parse(params.toDate):null

		List<Day> days = []
		Day day
		(fromDate..toDate).each{
			day = new Day([
						date:it,
						type:dayType,
						employee:requestor,
						status:"new",
						hours:8.0
					])
			if (day.validate()){
				day.save(flush:true)
				days.add(day)
			}//else day already exists
		}
		
		Request requestInstance = new Request([
					requestor:requestor,
					approver:approver,
					requestDate:new Date(),
					status:"new",
					days:days
				]).save()

		redirect(action:"list")
	}

	def approve(){
		Request requestInstance = Request.get(params.id)
		return [requestInstance:requestInstance]
	}

	def saveApproval(){
		Request requestInstance = Request.get(params.id)
		params.approvalDate = new Date()
		params.status = "approved"
		params.approver = Employee.findByUserId(session.user.userId)
		if(requestInstance){
			requestInstance.days.each{day->
				day.status = "approved"
				day.save()
			}
		}
		update()
	}

	def disapprove(){
		Request requestInstance = Request.get(params.id)
		return [requestInstance:requestInstance]
	}

	def saveDisapproval(){
		Request requestInstance = Request.get(params.id)
		params.approvalDate = new Date()
		params.status = "disapproved"
		params.approver = Employee.findByUserId(session.user.userId)
		if(requestInstance){
			requestInstance.days.each{day->
				day.status = "disapproved"
				day.save()
			}
		}
		update()
	}

	def cancel(){
		Request requestInstance = Request.get(params.id)
		return [requestInstance:requestInstance]
	}

	def saveCancel(){
		Request requestInstance = Request.get(params.id)
		params.status = "canceled"
		if(requestInstance){
			requestInstance.days.each{day->
				day.status = "canceled"
				day.save()
			}
		}
		update()
	}
}
